<?php

namespace App\Recharge\Api;

use App\Models\Admin\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use Illuminate\Http\Request;

class Chengtianxia extends AbstractApi
{

    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {
        $array = [
            'usrid'     => $bean->getSupplierNum(), //商户号
            'type'      => $bean->getGoodsId(),
            'buynum'    => $bean->getRechargeNum() * $bean->getStartCharging(),
            'ordernum'  => $bean->getOrderNo(), //订单号
            'rcaccount' => $bean->getAccount(),//充值账号
            'fromip'    => $bean->getIp(),//获取用户当前的ip
            'area'      => $bean->getGameAreaName(),
            'returl'    => $bean->getNotifyUrl(), //异步回调地址
        ];
        $array['sign'] = md5($array['usrid']. $array['type'].$array['buynum'].$array['ordernum'].$array['rcaccount']
            .$array['fromip']. $bean->getSecretKey());

        return $array;
    }

    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $status = $request->input('result');
        $order_sn = $request->input('ordernum');
        return $this->apiNotifyData($order_sn, $status)->setExecute(in_array($status, ['success', 'fail']))->setRechargeSuccess($status == 'success');
    }

    /**
     * @return mixed
     */
    public function apiNotifySuccess(): mixed
    {
        return response('OK');
    }
}
